import PlayerModel from 'server/model/playerModel';
import ItemModel from 'server/model/itemModel';
import FightAttri from 'server/model/fightAttri';
import playerStorage from 'js/core/playerStorage';

import {
    totalConfig,
    propName
} from 'server/config/templateConfig';

class PlayerHandle {
    static getInstance() {
        if (!PlayerHandle.instance) {
            PlayerHandle.instance = new PlayerHandle();
        }
        return PlayerHandle.instance;
    }

    //初始化角色信息
    initPlayer(player) {
        // this.calcAttri(player)
    }

    createPlayer() {
        let player = new PlayerModel();
        playerStorage.save(player);
        return player;
    }
    //计算人物属性
    calcAttri(player) {
        let basicAttri = player.basicAttri,
            basicInfo = player.basicInfo;
        let fightAttri = player.fightAttri;
        fightAttri.attack += basicAttri.attack; //攻击力
        fightAttri.maxHp += basicAttri.maxHp; //生命
        fightAttri.pdef += basicAttri.pdef; //防御
        fightAttri.ldef += basicAttri.ldef; //防御
        fightAttri.hp = fightAttri.maxHp;
        fightAttri.name = player.name;
    }

    initFightAttri(player) {
        let fightAttri = FightAttri.create(player.fightAttri),
            basicInfo = player.basicInfo;
        fightAttri.hp = player.fightAttri.maxHp;
        return fightAttri;
    }

    getFightAttri(player) {
        return player.fightAttri;
    }

    getFightAttriCopy(player) {
        return FightAttri.create(player.fightAttri)
    }

    getExp(player, exp) {
        player.basicInfo.exp += exp;
        let lvlexp = playerConfig.lvlexp[player.basicInfo.lvl - 1];
        if (player.basicInfo.exp >= lvlexp) {
            player.basicInfo.lvl += 1;
            this.calcAttri(player)
        }
    }
}

let playerHandle = PlayerHandle.getInstance();
export default playerHandle;